package com.jzo2o.customer.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证审核表 服务实现类
 * </p>
 *
 * @author itlxc
 * @since 2024-10-14
 */
@Service
@RequiredArgsConstructor
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements IWorkerCertificationAuditService {


    /**
     * 服务端提交认证申请
     * @param dto
     */
    @Override
    public void submit(WorkerCertificationAuditAddReqDTO dto) {
        Long userId = UserContext.currentUserId();
        if (ObjectUtil.isNull(userId)) {
            throw new ForbiddenOperationException("请登录后再操作");
        }
        // 判断身份证号是否重复
        WorkerCertificationAudit workerCertificationAudit = lambdaQuery().eq(WorkerCertificationAudit::getIdCardNo, dto.getIdCardNo()).one();
        if (ObjectUtil.isNotNull(workerCertificationAudit)) {
            throw new ForbiddenOperationException("您已经提交过认证申请");
        }
        workerCertificationAudit = new WorkerCertificationAudit();
        BeanUtil.copyProperties(dto, workerCertificationAudit);
        workerCertificationAudit.setServeProviderId(userId);
        boolean flag = this.save(workerCertificationAudit);
        if (!flag) {
            throw new ForbiddenOperationException("提交认证申请失败");
        }
    }

    /**
     * 查询最新的驳回原因
     * @return
     */
    @Override
    public String getRejectReason() {
        Long userId = UserContext.currentUserId();
        if (ObjectUtil.isNull(userId)) {
            throw new ForbiddenOperationException("请登录后再操作");
        }
        return this.lambdaQuery()
                .eq(WorkerCertificationAudit::getServeProviderId, userId)
                .one().getRejectReason();
    }

    /**
     * 审核服务人员认证分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult<WorkerCertificationAudit> pageQuery(WorkerCertificationAuditPageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto,
                () -> this.lambdaQuery()
                        .eq(ObjectUtil.isNotNull(dto.getAuditStatus()), WorkerCertificationAudit::getAuditStatus, dto.getAuditStatus())
                        .eq(ObjectUtil.isNotNull(dto.getCertificationStatus()), WorkerCertificationAudit::getCertificationStatus, dto.getCertificationStatus())
                        .eq(StrUtil.isNotBlank(dto.getIdCardNo()), WorkerCertificationAudit::getIdCardNo, dto.getIdCardNo())
                        .like(StrUtil.isNotBlank(dto.getName()), WorkerCertificationAudit::getName, dto.getName())
                        .list()

                );
    }

    /**
     * 审核服务人员认证信息
     * @param id
     * @param certificationStatus
     * @param rejectReason
     */
    @Override
    public void audit(Long id, Integer certificationStatus, String rejectReason) {
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        if (ObjectUtil.isNull(currentUserInfo)) {
            throw new ForbiddenOperationException("请登录后再操作");
        }
        WorkerCertificationAudit workerCertificationAudit = this.getById(id);
        if (ObjectUtil.isNull(workerCertificationAudit)) {
            throw new ForbiddenOperationException("该服务人员认证信息不存在");
        }
        workerCertificationAudit.setCertificationStatus(certificationStatus);
        workerCertificationAudit.setRejectReason(rejectReason);
        workerCertificationAudit.setAuditStatus(1);
        workerCertificationAudit.setAuditorId(currentUserInfo.getId());
        workerCertificationAudit.setAuditorName(currentUserInfo.getName());
        workerCertificationAudit.setAuditTime(LocalDateTime.now());
        boolean flag = this.updateById(workerCertificationAudit);
        if (!flag) {
            throw new ForbiddenOperationException("审核服务人员认证信息失败");
        }
    }
}
